﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ServiceStack.Common.Extensions;
using ServiceStack.Messaging.Rcon;
using ServiceStack.Redis;
using ServiceStack.Redis.Support;

namespace TestRedis
{
    /// <summary>
    ///封装redis,便于业务拓展
    /// </summary>
    public class RedisHelper
    {
        public static RedisClient redisClient { get; set; }
        ////object序列化方式存储
        private static ObjectSerializer serializer = new ObjectSerializer(); //位于namespace ServiceStack.Redis.Support;
        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="Ip"></param>
        /// <param name="prot"></param>
        public RedisHelper(string Ip, int prot)
        {
            redisClient = InitializeRedis(Ip, prot);
        }
        public RedisHelper() { }
        /// <summary>
        ///获取redis实列
        /// </summary>
        /// <param name="Ip">redis服务器ip</param>
        /// <param name="prot">redis端口</param>
        /// <returns></returns>
        private RedisClient InitializeRedis(string Ip, int prot)
        {
            return redisClient ?? new RedisClient(Ip, prot);
        }

        public RedisClient GetRedisClient()
        {
            return redisClient;
        }
        #region 添加数据

        /// <summary>
        /// 添加指定key集合数据
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value">value</param>
        public void AddItemToList(string key, string value)
        {
            redisClient.AddItemToList(key, value);
        }
        /// <summary>
        /// 添加指定key集合数据
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value">集合</param>
        public void AddItemToList<T>(string key, IEnumerable<T> value)
        {
            value.ForEach(x =>
            {
                redisClient.AddItemToList(key, x.ToString());
            });
        }
        /// <summary>
        /// 添加指定类型数据,key-value
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <param name="val"></param>
        /// <returns></returns>
        public bool TAddValue<T>(string key, T val)
        {
            return redisClient.Set<T>(key, val);
        }
        /// <summary>
        /// 添加对象数据
        /// </summary>
        /// <typeparam name="T">泛型T</typeparam>
        /// <param name="key">key</param>
        /// <param name="obj">对象</param>
        /// <returns></returns>
        public bool TAddObject<T>(string key, T obj)
        {
            return redisClient.Set<T>(key, obj);
        }
        /// <summary>
        /// 序列化对象添加,效率更高
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <param name="obj"></param>
        /// <returns></returns>
        public bool TAddObjectSerialize<T>(string key, T obj)
        {

            return redisClient.Set<byte[]>(key, serializer.Serialize(obj));
        }

        public void TSerializerToFile<T>(T obj, string path)
        {
            var json = serializer.Serialize(obj);
            string sRespons = json.ToString();

            byte[] byt = System.Text.Encoding.UTF8.GetBytes(sRespons);
            FileStream fs = new FileStream(path, FileMode.OpenOrCreate);
            fs.Write(byt, 0, byt.Length);
            fs.Dispose();
            fs.Close();
        }
        #endregion

        #region 读取数据
        /// <summary>
        /// 读取指定Key集合数据
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public IEnumerable<string> GetListForKey(string key)
        {
            return redisClient.GetAllItemsFromList(key);
        }
        /// <summary>
        /// 获取序列化后的对象数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <returns></returns>
        public T TGetSerializeObjForKey<T>(string key)
        {
            return (T)serializer.Deserialize(redisClient.Get<byte[]>(key));
        }
        /// <summary>
        /// 获取指定key数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <returns></returns>
        public T TGetValeForKey<T>(string key)
        {
            return redisClient.Get<T>(key);
        }

        public T TGetValueToFile<T>(string path)
        {
            FileStream fs = new FileStream(path, FileMode.Open);
            byte[] byt = new byte[fs.Length];
            fs.Read(byt, 0, byt.Length);
            var obj = (T)serializer.Deserialize(redisClient.Get<byte[]>(Encoding.UTF8.GetString(byt)));
            fs.Dispose();
            fs.Close();
            return obj;
        }
        #endregion

    }
}
